<template>
  <div class="user">
    <el-form
      ref="ruleForm"
      :rules="rules"
      label-position="left"
      label-width="120px"
      :model="ruleForm"
    >
      <el-form-item label="已绑定：">
        <p>{{ userInfo.phone }}</p>
      </el-form-item>
      <el-form-item label="当前账号密码：" prop="password">
        <el-input
          v-model="ruleForm.password"
          placeholder="请输入当前账号密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="新手机号：" prop="newPhone">
        <el-input v-model="ruleForm.newPhone" placeholder="请输入新手机号">
        </el-input>
      </el-form-item>
      <el-form-item label="短信校验码：" prop="smsCode" class="code">
        <el-input v-model="ruleForm.smsCode" placeholder="请输入短信校验码">
        </el-input>
        <el-button
          class="code-btn code"
          style="width: 110px"
          :disabled="isDisabled"
          type="primary"
          @click="sendCode"
          >{{ buttonText }}</el-button
        >
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button class="submit" @click="submit()" type="primary"
        >提交</el-button
      >
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},
  data() {
    const validateUserPhone = (rule, value, callback) => {
      if (!/^1[3|4|5|7|8|9][0-9]\d{8}$/.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    return {
      buttonText: "获取验证码",
      isDisabled: false, // 是否禁止点击发送验证码按钮
      ruleForm: {
        password: "",
        // code: "",
        newPhone: "",
        smsCode: "",
      },
      rules: {
        password: [
          { required: true, message: "请输入当前账号密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        newPhone: [
          { required: true, message: "请输入新手机号", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "请输入正确手机号",
            validator: validateUserPhone,
          },
        ],
        smsCode: [
          { required: true, message: "请输入短信校验码", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    ...mapActions(["sendSms", "changePhone", "getUserInfo"]),
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let data = {
            password: this.ruleForm.password,
            new_phone: this.ruleForm.newPhone,
            phone_code: this.ruleForm.smsCode,
          };
          this.changePhone(data).then((res) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.$refs.ruleForm.resetFields();
            this.getUserInfo();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sendCode() {
      this.$refs.ruleForm.validateField("newPhone", (phoneError) => {
        if (!phoneError) {
          this.sendSms({ mobile: this.ruleForm.newPhone, type: 2 }).then(
            (res) => {
              this.changeBtnText();
            }
          );
        }
      });
    },
    changeBtnText() {
      let time = 60;
      this.isDisabled = true;
      this.buttonText = "已发送（" + time + " s）";
      let timer = setInterval(() => {
        time--;
        this.buttonText = "已发送（" + time + " s）";
        if (time === 0) {
          clearInterval(timer);
          this.buttonText = "重新获取";
          this.isDisabled = false;
        }
      }, 1000);
    },
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
};
</script>

<style scoped>
.user {
  width: 100%;
  background: #fff;
  box-sizing: border-box;
  padding: 0 0 20px 0;
}
.user >>> .el-input__inner {
  border: none;
  width: 100%;
  font-size: 14px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #999999;
  padding: 0;
}
.user >>> .el-select {
  width: 100%;
}
.user >>> .el-form-item {
  width: 92%;
  border-bottom: 1px solid #e6e6e6;
  margin: 8px 15px 0;
  padding-bottom: 10px;
}
.submit {
  width:94%;
  height: 45px;
  background: #0e93e4;
  border-radius: 3px;
  font-size: 18px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: #ffffff;
  margin: 30px auto 60px;
  display: block;
}
.code >>> .el-form-item__content {
  flex: 1;
}
.code .el-button {
  width: 110px;
  padding: 0;
  height: 38px;
  position: absolute;
  right: 0;
  bottom: 0;
}
</style>